;Understand, Correct, Improve           ___
;________/| _________________/\__/\____/  /_____
;\  ____/ |/   __/  /  / __ /  \/  \  \  /   __/
;|   __/  /\__   \    /  __ \      /     \  _/ \
;|___\ \__\____  //__/\_____/\    /__/\  /_____/
;+-------------\/breeze'03----\  /crew-\/------+
;                              \/
; Fishb0ne borntro 2008 [Pentagon 1024SL 2.2]
; Copyright (C) 2008 Fishbone CREW All right reserved
; Written by breeze \ fishbone crew | http://twilight.org.ru
; breeze@tut.by
; 29.01.2008

; Player TurboSound FM modules

;		ld 	hl,tsfmMusic
;		call 	plInit			; init player
;
;		ei
;loop		halt  
;		ld 	a,#04
;		out 	(#fe),a
;		call 	plPlay			; play next note
;		ld 	a,#00
;		out 	(#fe),a
;		ld 	a,#7f
;		in 	a,(#fe)
;		rra
;		jr	nc,endX			; stop player
;		jp 	loop
; 
;endX		call	plStop
;		ld	a,6
;		out	(254),a
;		jr	$


;--------------------------------------------------------
; Standart header like as ProTracker Players
;--------------------------------------------------------
plInit		jp	tsfmPlayer
plPlay		jp	tsfmPlay
plStop		jp	tsfmStop
;--------------------------------------------------------
tsfmPlayer	ex 	de,hl
		ld 	hl,#0009
		add 	hl,de
		ld 	a,(hl)
		inc 	hl
		cp 	#32
		ld 	a,#c9
		jr 	z,$+4

		ld 	a,#3e
		ld 	(tsfm60Hz),a
		exx  
		ld 	hl,tsfmMiniTab
		ld 	bc,#06fd
tsfmMini0	ld 	de,tsfmMiniHL+1
		ldi  
		ldi  
		exx  
		ld 	a,(hl)
		inc 	hl
		push 	hl
		ld 	h,(hl)
		ld 	l,a
		add 	hl,de
		;ld 	(addrF),hl
tsfmMiniHL	ld 	(0),hl
		pop 	hl
		inc 	hl
		exx  
		djnz 	tsfmMini0

		xor 	a
		ld 	(blkCntA),a
		ld 	(blkCntB),a
		ld 	(blkCntC),a
		ld 	(blkCntD),a
		ld 	(blkCntE),a
		ld 	(blkCntF),a
		dec 	a
		ld 	(skipA),a
		ld 	(skipB),a
		ld 	(skipC),a
		ld 	(skipD),a
		ld 	(skipE),a
		ld 	(skipF),a
;-------------------------------------------------------------------
tsfmStop	ld 	de,#ffbf
		ld 	c,#fd
		call 	selChip0
		call 	tsfmMiniPP
		call 	selChip1
tsfmMiniPP	xor a
		exa  
		ld 	a,#0d
regClrS		call 	writeReg
		dec 	a
		jp 	p,regClrS

		ld 	a,#b3
regClrZ		cp 	#4f
		jr 	nz,$+4

		ld 	a,#3f
		call 	writeReg
		dec 	a
		cp 	#30
		jr 	nc,regClrZ

		ld 	a,#0f
		exa  
		ld 	a,#8f
regClrR		call 	writeReg
		dec 	a
		jp 	m,regClrR

		xor 	a
		exa  
		ld 	a,#28
		call 	writeReg
		exa  
		inc 	a
		exa  
		call 	writeReg
		exa  
		inc 	a
		exa  
		call 	writeReg
		dec 	a
		call 	writeReg
		ld 	a,#7f
		exa  
		ld 	a,#4f
regClrT		call 	writeReg
		dec 	a
		cp 	#40
		jr 	nc,regClrT

		ld 	a,#2f
		call 	writeReg
		ld 	a,#2d
writeReg	ld 	b,d
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		exa
		; Wait Status Begin  
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		exa  
		ret  
;---------------------------------------        
selChip0	ld 	a,#f8
		ld 	b,d     
		out 	(C),a
		ret

selChip1	ld 	a,#f9
		ld 	b,d
		out 	(C),a
		ret  

		push 	bc
		ld 	bc,#7ffd
		out 	(C),a
		pop 	bc
		ret  

tsfmPlay	ld 	de,#ffbf
		ld 	c,#fd
		ld 	b,d
		ld 	a,#f8
		out 	(C),a
		call 	tsfmA
		call 	tsfmB
		call 	tsfmC
		ld 	b,d
		ld 	a,#f9
		out 	(C),a
		call 	tsfmD
		call 	tsfmE
		call 	tsfmF
tsfm60Hz
cnt60		ld	a,6
		dec	a
		jr 	nz,$+4
		ld 	a,#06
		ld 	(cnt60+1),a
		jr 	z,tsfmPlay
		ret  
;-----------------------------------------
regsX		ld 	b,d
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		outi  
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		outi  
		dec 	a
		jr 	nz,regsX
		ret
;-----------------------------------------  
blockA		ld 	a,(hl)
		inc 	hl
		ld 	(blkCntA),a
		ld 	b,(hl)
		inc 	hl
		ld 	c,(hl)
		inc 	hl
		ld 	(blkRetAddrA),hl
		add 	hl,bc
		ld 	c,#fd
		jp 	tsfmFrameA

oldFarA		ld 	b,(hl)
		inc 	hl
oldNearA	ld 	c,(hl)
		inc 	hl
		push 	hl
		add 	hl,bc
		ld 	c,#fd
		call 	tsfmFrameA
		pop 	hl
		ld 	(addrA),hl
		ret

hlSkiperA	jr 	z,oldFarA
		cp 	#e0
		jr 	c,slideA
		ld 	b,a
		cp 	#ff
		jr 	z,oldNearA
		ld 	(addrA),hl
skiperA		ld 	(skipA),a
		ret
  
slideA		add 	a,#30
		jr 	z,blockA
		add 	a,#c3
tsfmSlowA=$-#01
		ld 	(tsfmSlowA),a
		ld 	(addrA),hl
		ld 	b,d
		ld 	hl,#22a4
tsfmHighA=$-#01
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),l
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),h
		ld 	b,d
		ld 	l,#a0
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),l
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ret 
 
beginA		ld 	(loopAddrA),hl
		jp 	tsfmFrameA
endA		ld 	hl,0		;#6b2d
loopAddrA=$-#02
		jp tsfmFrameA
tsfmA		ld 	a,#fd
skipA=$-#01
		inc 	a
		jr 	nz,skiperA
		ld 	hl,0		;#6b5e
addrA=$-#02
		or #00
blkCntA=$-#01
		jr 	z,tsfmFrameA
		dec 	a
		ld 	(blkCntA),a
		jr 	nz,tsfmFrameA
		ld 	hl,0		;#6b49
blkRetAddrA=$-#02
tsfmFrameA	ld 	a,(hl)
		inc 	hl
		cp 	#7e
		jr 	z,beginA
		cp 	#7f
		jr 	z,endA
		cp 	e
		jr 	nc,hlSkiperA
		jp 	p,LL63df
		exa  
		ld 	b,d
		ld 	a,#28
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		xor 	a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out (	C),a
		exa  
LL63df		or a
		push af
		rra  
		jr 	nc,LL6414
		exa  
		ld 	b,d
		ld 	a,#a4
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,(hl)
		inc 	hl
		ld 	(tsfmHighA),a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ld 	b,d
		ld 	a,#a0
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,(hl)
		inc 	hl
		ld 	(tsfmSlowA),a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		exa  
LL6414		and 	#1f
		call 	nz,regsX
		ld 	(addrA),hl
		pop 	af
		ret p

		ld 	b,d
		ld 	a,#28
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,#f0
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ret
;-----------------------------------------------------------  
blockB		ld 	a,(hl)
		inc 	hl
		ld 	(blkCntB),a
		ld 	b,(hl)
		inc 	hl
		ld 	c,(hl)
		inc 	hl
		ld 	(blkRetAddrB),hl
		add 	hl,bc
		ld 	c,#fd
		jp 	tsfmFrameB

oldFarB		ld 	b,(hl)
		inc 	hl
oldNearB	ld 	c,(hl)
		inc 	hl
		push 	hl
		add 	hl,bc
		ld 	c,#fd
		call 	tsfmFrameB
		pop 	hl
		ld 	(addrB),hl
		ret  

hlSkiperB	jr 	z,oldFarB
		cp 	#e0
		jr 	c,slideB
		ld 	b,a
		cp 	#ff
		jr 	z,oldNearB
		ld 	(addrB),hl
skiperB		ld 	(skipB),a
		ret
  
slideB		add 	a,#30
		jr 	z,blockB
		add 	a,#af
tsfmSlowB=$-#01
		ld 	(tsfmSlowB),a
		ld 	(addrB),hl
		ld 	b,d
		ld 	hl,#13a5
tsfmHighB=$-#01
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),l
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),h
		ld 	b,d
		ld 	l,#a1
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),l
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ret
  
beginB		ld 	(loopAddrB),hl
		jp 	tsfmFrameB

endB		ld 	hl,0			;#6dec
loopAddrB=$-#02
		jp 	tsfmFrameB
tsfmB		ld 	a,#ec
skipB=$-#01
		inc 	a
		jr 	nz,skiperB
		ld 	hl,0			;#6df0
addrB=$-#02
		or 	#00
blkCntB=$-#01
		jr 	z,tsfmFrameB
		dec 	a
		ld 	(blkCntB),a
		jr 	nz,tsfmFrameB
		ld 	hl,0			;#6f9f
blkRetAddrB=$-#02
tsfmFrameB	ld 	a,(hl)
		inc 	hl
		cp 	#7e
		jr 	z,beginB
		cp 	#7f
		jr 	z,endB
		cp 	e
		jr 	nc,hlSkiperB
		jp 	p,LL64e0
		exa  
		ld 	b,d
		ld a,#28
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,#01
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		exa  
LL64e0		or 	a
		push 	af
		rra  
		jr 	nc,LL6515
		exa  
		ld 	b,d
		ld 	a,#a5
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,(hl)
		inc 	hl
		ld 	(tsfmHighB),a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ld 	b,d
		ld 	a,#a1
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,(hl)
		inc 	hl
		ld 	(tsfmSlowB),a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		exa  
LL6515		and 	#1f
		call 	nz,regsX
		ld 	(addrB),hl
		pop 	af
		ret 	p

		ld 	b,d
		ld 	a,#28
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,#f1
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ret  
;--------------------------------------------------
blockC		ld 	a,(hl)
		inc 	hl
		ld 	(blkCntC),a
		ld 	b,(hl)
		inc 	hl
		ld 	c,(hl)
		inc 	hl
		ld 	(blkRetAddrC),hl
		add 	hl,bc
		ld 	c,#fd
		jp 	tsfmFrameC

oldFarC		ld 	b,(hl)
		inc 	hl
oldNearC	ld 	c,(hl)
		inc 	hl
		push 	hl
		add 	hl,bc
		ld 	c,#fd
		call 	tsfmFrameC
		pop 	hl
		ld 	(addrC),hl
		ret

hlSkiperC	jr 	z,oldFarC
		cp 	#e0
		jr 	c,slideC
		ld 	b,a
		cp 	#ff
		jr 	z,oldNearC
		ld 	(addrC),hl
skiperC		ld 	(skipC),a
		ret  

slideC		add 	a,#30
		jr 	z,blockC
		add 	a,#c3
tsfmSlowC=$-#01
		ld 	(tsfmSlowC),a
		ld 	(addrC),hl
		ld 	b,d
		ld 	hl,#1aa6
tsfmHighC=$-#01
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out (C),l
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),h
		ld 	b,d
		ld 	l,#a2
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out (C),l
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ret

beginC		ld (loopAddrC),hl
		jp tsfmFrameC

endC		ld 	hl,0		;#7138
loopAddrC=$-#02
		jp 	tsfmFrameC
tsfmC		ld 	a,#ec
skipC=$-#01
		inc 	a
		jr 	nz,skiperC
		ld 	hl,0		;#713c
addrC=$-#02
		or #00
blkCntC=$-#01
		jr 	z,tsfmFrameC
		dec 	a
		ld 	(blkCntC),a
		jr 	nz,tsfmFrameC
		ld 	hl,0		;#73a9
blkRetAddrC=$-#02
tsfmFrameC	ld 	a,(hl)
		inc 	hl
		cp 	#7e
		jr 	z,beginC
		cp 	#7f
		jr 	z,endC
		cp 	e
		jr 	nc,hlSkiperC
		jp 	p,LL65e1
		exa  
		ld 	b,d
		ld 	a,#28
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,#02
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		exa  
LL65e1		or 	a
		push 	af
		rra  
		jr 	nc,LL6616
		exa  
		ld 	b,d
		ld 	a,#a6
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,(hl)
		inc 	hl
		ld 	(tsfmHighC),a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ld 	b,d
		ld 	a,#a2
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,(hl)
		inc 	hl
		ld 	(tsfmSlowC),a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		exa  
LL6616		and 	#1f
		call 	nz,regsX
		ld 	(addrC),hl
		pop 	af
		ret 	p

		ld 	b,d
		ld 	a,#28
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,#f2
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ret
;--------------------------------------------  
blockD		ld 	a,(hl)
		inc 	hl
		ld 	(blkCntD),a
		ld 	b,(hl)
		inc 	hl
		ld 	c,(hl)
		inc 	hl
		ld 	(blkRetAddrD),hl
		add 	hl,bc
		ld 	c,#fd
		jp 	tsfmFrameD

oldFarD		ld 	b,(hl)
		inc 	hl
oldNearD	ld 	c,(hl)
		inc 	hl
		push 	hl
		add 	hl,bc
		ld 	c,#fd
		call 	tsfmFrameD
		pop 	hl
		ld 	(addrD),hl
		ret

hlSkiperD	jr 	z,oldFarD
		cp 	#e0
		jr 	c,slideD
		ld 	b,a
		cp 	#ff
		jr 	z,oldNearD
		ld 	(addrD),hl
skiperD		ld 	(skipD),a
		ret
  
slideD		add	a,#30
		jr 	z,blockD
		add 	a,#c3
tsfmSlowD=$-#01
		ld 	(tsfmSlowD),a
		ld 	(addrD),hl
		ld 	b,d
		ld 	hl,#1aa4
tsfmHighD=$-#01
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out (C),l
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),h
		ld 	b,d
		ld 	l,#a0
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),l
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ret  

beginD		ld 	(loopAddrD),hl
		jp 	tsfmFrameD
endD		ld 	hl,0			;#7633
loopAddrD=$-#02
		jp 	tsfmFrameD
tsfmD		ld 	a,#fe
skipD=$-#01
		inc 	a
		jr 	nz,skiperD
		ld 	hl,0			;#7673
addrD=$-#02
		or 	#00
blkCntD=$-#01
		jr	z,tsfmFrameD
		dec 	a
		ld 	(blkCntD),a
		jr 	nz,tsfmFrameD
		ld 	hl,0			;#78ad
blkRetAddrD=$-#02
tsfmFrameD	ld 	a,(hl)
		inc 	hl
		cp 	#7e
		jr 	z,beginD
		cp 	#7f
		jr 	z,endD
		cp 	e
		jr 	nc,hlSkiperD
		jp 	p,LL66e1
		exa  
		ld 	b,d
		ld 	a,#28
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		xor	a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		exa  
LL66e1		or 	a
		push 	af
		rra  
		jr	nc,LL6716
		exa  
		ld 	b,d
		ld 	a,#a4
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,(hl)
		inc 	hl
		ld 	(tsfmHighD),a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ld 	b,d
		ld 	a,#a0
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,(hl)
		inc 	hl
		ld 	(tsfmSlowD),a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		exa  
LL6716		and 	#1f
		call 	nz,regsX
		ld 	(addrD),hl
		pop 	af
		ret 	p

		ld 	b,d
		ld 	a,#28
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,#f0
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ret
;--------------------------------------------------  
blockE		ld 	a,(hl)
		inc 	hl
		ld 	(blkCntE),a
		ld 	b,(hl)
		inc 	hl
		ld 	c,(hl)
		inc 	hl
		ld 	(blkRetAddrE),hl
		add 	hl,bc
		ld 	c,#fd
		jp 	tsfmFrameE

oldFarE		ld 	b,(hl)
		inc 	hl
oldNearE	ld 	c,(hl)
		inc 	hl
		push 	hl
		add 	hl,bc
		ld 	c,#fd
		call 	tsfmFrameE
		pop 	hl
		ld 	(addrE),hl
		ret

hlSkiperE	jr 	z,oldFarE
		cp 	#e0
		jr 	c,slideE
		ld 	b,a
		cp 	#ff
		jr 	z,oldNearE
		ld 	(addrE),hl
skiperE		ld 	(skipE),a
		ret

slideE		add 	a,#30
		jr 	z,blockE
		add 	a,#af
tsfmSlowE=$-#01
		ld 	(tsfmSlowE),a
		ld 	(addrE),hl
		ld 	b,d
		ld 	hl,#1ba5
tsfmHighE=$-#01
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),l
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),h
		ld 	b,d
		ld 	l,#a1
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),l
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ret

beginE		ld 	(loopAddrE),hl
		jp 	tsfmFrameE

endE		ld 	hl,0			;#7aef
loopAddrE=$-#02
		jp 	tsfmFrameE

tsfmE		ld 	a,#fd
skipE=$-#01
		inc 	a
		jr 	nz,skiperE

		ld 	hl,0			;#7b37
addrE=$-#02
		or 	#00
blkCntE=$-#01
		jr 	z,tsfmFrameE
		dec 	a
		ld 	(blkCntE),a
		jr 	nz,tsfmFrameE
		ld 	hl,0			;#7b9c
blkRetAddrE=$-#02
tsfmFrameE	ld 	a,(hl)
		inc 	hl
		cp 	#7e
		jr 	z,beginE
		cp 	#7f
		jr 	z,endE
		cp 	e
		jr 	nc,hlSkiperE
		jp 	p,LL67e2
		exa  
		ld 	b,d
		ld 	a,#28
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,#01
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		exa  
LL67e2		or 	a
		push 	af
		rra  
		jr 	nc,LL6817
		exa  
		ld 	b,d
		ld 	a,#a5
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,(hl)
		inc 	hl
		ld 	(tsfmHighE),a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ld 	b,d
		ld	a,#a1
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,(hl)
		inc 	hl
		ld 	(tsfmSlowE),a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		exa  
LL6817		and 	#1f
		call 	nz,regsX
		ld 	(addrE),hl
		pop 	af
		ret 	p

		ld 	b,d
		ld 	a,#28
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,#f1
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ret
;--------------------------------------------------  
blockF		ld 	a,(hl)
		inc 	hl
		ld 	(blkCntF),a
		ld 	b,(hl)
		inc 	hl
		ld 	c,(hl)
		inc 	hl
		ld 	(blkRetAddrF),hl
		add 	hl,bc
		ld 	c,#fd
		jp 	tsfmFrameF

oldFarF		ld 	b,(hl)
		inc 	hl
oldNearF	ld 	c,(hl)
		inc 	hl
		push 	hl
		add 	hl,bc
		ld 	c,#fd
		call 	tsfmFrameF
		pop 	hl
		ld 	(addrF),hl
		ret

hlSkiperF	jr 	z,oldFarF
		cp 	#e0
		jr 	c,slideF
		ld 	b,a
		cp 	#ff
		jr 	z,oldNearF
		ld 	(addrF),hl
skiperF		ld 	(skipF),a
		ret

slideF		add 	a,#30
		jr 	z,blockF
		add 	a,#af
tsfmSlowF=$-#01
		ld 	(tsfmSlowF),a
		ld 	(addrF),hl
		ld 	b,d
		ld 	hl,#23a6
tsfmHighF=$-#01
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out (C),l
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),h
		ld 	b,d
		ld 	l,#a2
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),l
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ret

beginF		ld 	(loopAddrF),hl
		jp 	tsfmFrameF

endF		ld 	hl,0			;#7c95
loopAddrF=$-#02
		jp 	tsfmFrameF

tsfmF		ld 	a,#ff
skipF=$-#01
		inc 	a
		jr 	nz,skiperF
		ld 	hl,0			;#7cd9
addrF=$-#02
		or 	#00
blkCntF=$-#01
		jr 	z,tsfmFrameF
		dec 	a
		ld 	(blkCntF),a
		jr 	nz,tsfmFrameF
		ld 	hl,0			;#7cc0
blkRetAddrF=$-#02
tsfmFrameF	ld 	a,(hl)
		inc 	hl
		cp 	#7e
		jr 	z,beginF
		cp 	#7f
		jr 	z,endF
		cp 	e
		jr 	nc,hlSkiperF
		jp 	p,LL68e3
		exa  
		ld 	b,d
		ld 	a,#28
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,#02
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld	b,e
		out 	(C),a
		exa  
LL68e3		or 	a
		push 	af
		rra  
		jr 	nc,LL6918
		exa  
		ld 	b,d
		ld 	a,#a6
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,(hl)
		inc 	hl
		ld 	(tsfmHighF),a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		ld 	b,d
		ld 	a,#a2
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,(hl)
		inc 	hl
		ld 	(tsfmSlowF),a
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld 	b,e
		out 	(C),a
		exa  
LL6918		and 	#1f
		call 	nz,regsX
		ld 	(addrF),hl
		pop 	af
		ret 	p

		ld 	b,d
		ld 	a,#28
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		out 	(C),a
		ld 	a,#f2
		; Wait Status Begin
		db 	#ed,#70  
		jp 	m,$-2
		; Wait Status End
		ld	b,e
		out 	(C),a
		ret
;---------------------------------------
tsfmMiniTab	
		DW	addrA
		DW	addrB
		DW	addrC
		DW	addrD
		DW	addrE
		DW	addrF
;---------------------------------------
tsfmMusic	incbin "borntro.tfc"
